package com.tg.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tg.demo.config.SysUserContext;
import com.tg.demo.entity.Activity;
import com.tg.demo.entity.ActivityItem;
import com.tg.demo.entity.ActivityRecord;
import com.tg.demo.entity.User;
import com.tg.demo.mapper.ActivityItemMapper;
import com.tg.demo.mapper.ActivityMapper;
import com.tg.demo.mapper.ActivityRecordMapper;
import com.tg.demo.service.ActivityService;
import com.tg.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户信息表ServiceImpl
 *
 * @author yuanjin
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    private static final Logger logger = LoggerFactory.getLogger(ActivityServiceImpl.class);
    @Resource
    private UserService userService;
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private ActivityItemMapper activityItemMapper;
    @Resource
    private ActivityRecordMapper activityRecordMapper;


    @Override
    public Activity getActivityById(Integer id) {
        Activity activity = activityMapper.getActivityById(id);
        List<ActivityItem> items = activityItemMapper.getListByCondition(
                ActivityItem.builder().activityId(id).build()
        );
        activity.setItems(items);
        return activity;
    }

    @Override
    public List<Activity> getListByCondition(Activity activity) {
        return activityMapper.getListByCondition(activity);
    }

    @Override
    public PageInfo<Activity> getListByPage(Activity activity, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Activity> list = this.getListByCondition(activity);
        for (Activity item : list) {
            User user = userService.getUserByUserCode(item.getCreateUser());
            item.setCreateUserName(user.getUserName());
        }
        return PageInfo.of(list);
    }

    @Override
    public void update(Activity activity) {
        List<ActivityItem> items = activity.getItems();
        if (null != activity.getId()) {
            activityItemMapper.delete(activity.getId());
        }
        for (ActivityItem ele : items) {
            ActivityItem item = ActivityItem.builder()
                    .activityId(activity.getId())
                    .itemValue(ele.getItemValue())
                    .yn(1)
                    .build();
            activityItemMapper.insert(item);
        }
        activityMapper.update(activity);
    }

    @Override
    public void insert(Activity activity) {
        List<ActivityItem> items = activity.getItems();
        activityMapper.insert(activity);
        for (ActivityItem ele : items) {
            ActivityItem item = ActivityItem.builder()
                    .activityId(activity.getId())
                    .itemValue(ele.getItemValue())
                    .yn(1)
                    .build();
            activityItemMapper.insert(item);
        }
    }

    @Override
    public Integer delete(Integer id) {
        return activityMapper.delete(id);
    }

    @Override
    public List<Activity> getListToUser(String userCode) {
        User user = SysUserContext.getSysUser();
        List<Activity> list = activityMapper.getListToUser();
        if (list.isEmpty()) {
            return null;
        }
        List<ActivityRecord> records = activityRecordMapper.getListByCondition(
                ActivityRecord.builder().userCode(userCode).build()
        );
        List<Activity> result = new ArrayList<>();
        for (Activity ele : list) {
            List<ActivityItem> items = activityItemMapper.getListByCondition(
                    ActivityItem.builder().activityId(ele.getId()).build());
            if (!items.isEmpty()) {
                ele.setItems(items);
            }
            if (!records.isEmpty()) {
                for (ActivityRecord record : records) {
                    if (ele.getId().equals(record.getActivityId())) {
                        ele.setRecord(record);
                        ele.setRecordId(record.getId());
                        ele.setItemId(record.getItemId());
                        ele.setExplainInfo(record.getExplainInfo());
                        ele.setIsJoin(1);
                        break;
                    }
                }
            }
            boolean isAdd = ele.getRanging().equals(user.getDepartment())
                    || ele.getRanging().equals(user.getProfessional())
                    || ele.getRanging().equals(user.getClassRoom())
                    || ele.getRanging().equals("全体学生");
            if (isAdd) {
                result.add(ele);
            }
        }
        return result;
    }

    @Override
    public void updateRecord(ActivityRecord activityRecord) {
        List<ActivityRecord> records = activityRecordMapper.getListByCondition(
                ActivityRecord.builder().id(activityRecord.getId()).build()
        );
        ActivityRecord record = records.get(0);
        record.setItemId(activityRecord.getItemId());
        record.setExplainInfo(activityRecord.getExplainInfo());
        record.setCreateTime(new Date());
        activityRecordMapper.update(record);
    }

    @Override
    public void insertRecord(ActivityRecord activityRecord) {
        ActivityRecord record = ActivityRecord.builder()
                .activityId(activityRecord.getActivityId())
                .userCode(activityRecord.getUserCode())
                .userName(activityRecord.getUserName())
                .itemId(activityRecord.getItemId())
                .explainInfo(activityRecord.getExplainInfo())
                .createTime(new Date())
                .yn(1)
                .build();
        activityRecordMapper.insert(record);
    }

    @Override
    public PageInfo<ActivityRecord> getRecordListByPage(ActivityRecord activityRecord, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ActivityRecord> list = activityRecordMapper.getListByCondition(activityRecord);
        return PageInfo.of(list);
    }
}
